<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class admin_controller_group extends omnitrix_controller
{
	public function index_action()
	{
		/*
		$sql = 'SELECT g.group_id, g.group_name, g.group_desc
					COUNT(1) AS user_count
				FROM #system_group# g
				LEFT JOIN #system_user# u ON u.group_id = g.group_id
				GROUP BY g.group_id, g.group_name';
		*/
		$sql = 'SELECT g.group_id, g.group_name, g.group_desc,
					0 AS user_count
				FROM #system_group# g';
		extract(otx_db_list_query($sql, null, 'g.group_id', 'asc'));

		$list_table = otx_html_create_table($this->_('heading_index'), 'table-list', $pager);
		$list_table->set_head_cell($this->_('column_group_name'));
		$list_table->set_head_cell($this->_('column_user_count'), array('class' => 'center', 'width' => 150));
		$list_table->set_head_cell($this->_('text_actions'), array('class' => 'center', 'width' => 150));

		if ($rows) {
			foreach ($rows as $row) {
				$group_detail = otx_html_strong(otx_html_link($row['group_name'], array('href' => otx_router_url('edit', null, null, array('id' => $row['group_id'])))));
				if (otx_validate_not_empty($row['group_desc'])) {
					$group_detail .= '<br />' . otx_html_small($row['group_desc']);
				}

				$actions = otx_html_tag_custom('dropdown');
				$actions->add_link(otx_html_link($this->_('action_edit'), array('href' => otx_router_url('edit', null, null, array('id' => $row['group_id'])))));
				if ($row['group_id'] <> 1) {
					$actions->add_link(otx_html_link($this->_('action_permissions'), array('href' => otx_router_url('permissions', null, null, array('id' => $row['group_id'])))));
				}
				if ($row['group_id'] > 2) {
					$actions->add_link(otx_html_link($this->_('action_delete'), array('href' => otx_router_url('delete', null, null, array('id' => $row['group_id'])), 'data-confirm' => 'confirm_delete')));
				}

				$list_table->add_body_row();
				$list_table->set_body_cell($group_detail);
				$list_table->set_body_cell($row['user_count'], array('class' => 'center'));
				$list_table->set_body_cell($actions->render_link($this->_('text_options')), array('class' => 'center actions'));
			};
		} else {
			$list_table->add_body_row();
			$list_table->set_body_cell($this->_('text_no_results'), array('colspan' => 2));
		}

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'), true);
		otx_html_add_navtab($this->_('navtab_add_title'), $this->_('navtab_add_desc'), otx_router_url('add'));
		otx_html_add_breadcrumb($this->_('breadcrumb_index'));
		otx_html_response_title($this->_('breadcrumb_index'));
	}

	public function add_action()
	{
		$this->edit_action();
	}

	public function edit_action()
	{
		$group_id = otx_param_int('id');

		if ($group_id) {
			$sql = 'SELECT * FROM #system_group# WHERE group_id = ?';
			$row = otx_db_fetch_row($sql, $group_id);
			if (false == $row) {
				otx_admin_flash_warning($this->_('error_invalid_id', $group_id));
				otx_router_redirect('index');
			}
		}

		if (otx_request_is_post()) {
			$vars['group_name'] = otx_param_string('group_name');
			$vars['group_desc'] = otx_param_string('group_desc');

			if (otx_validate_is_empty($vars['group_name'])) {
				$errors[] = $this->_('error_group_name_empty');
			} else {
				if (($group_id && otx_validate_field_count('#system_group#', 'group_name', $vars['group_name'], 'group_id <> ' . $group_id) > 0) ||
					(!$group_id && otx_validate_field_count('#system_group#', 'group_name', $vars['group_name']) > 0)) {
					$errors[] = $this->_('error_group_name_exists');
				}
			}

			if (empty($errors)) {
				if ($group_id) {
					otx_db_update('#system_group#', $vars, array('group_id' => $group_id));

					$vars['group_id'] = $group_id;
					otx_admin_flash_message($this->_('text_success_edit', $vars));
					otx_admin_log_action($this->_('text_success_edit', $vars));
				} else {
					otx_db_insert('#system_group#', $vars);

					$vars['group_id'] = otx_db_last_inserted_id();
					otx_admin_flash_message($this->_('text_success_add', $vars));
					otx_admin_log_action($this->_('text_success_add', $vars));
				}

				otx_router_redirect('index');
			}
		} else {
			if ($group_id) {
				$vars['group_name'] = $row['group_name'];
				$vars['group_desc'] = $row['group_desc'];
			} else {
				$vars['group_name'] = '';
				$vars['group_desc'] = '';
			}

			$errors = array();
		}

		$form_wrapper = otx_html_create_wrapper()->set_form_errors($errors);
		$form_table = $form_wrapper->create_table($group_id ? $this->_('navtab_edit_title') : $this->_('navtab_add_title'), 'table-form');
		$form_table->add_form_entry($this->_('entry_group_name'), otx_html_form_text('group_name', $vars['group_name']));
		$form_table->add_form_entry($this->_('entry_group_desc'), otx_html_form_text('group_desc', $vars['group_desc']));

		otx_html_add_breadcrumb($this->_('breadcrumb_index'), otx_router_url('index'));
		if ($group_id) {
			otx_html_add_breadcrumb($this->_('breadcrumb_edit', $row));
			otx_html_response_title($this->_('breadcrumb_edit', $row));
			otx_html_add_navtab($this->_('navtab_edit_title'), $this->_('navtab_edit_desc'), otx_request_uri(), true);
		} else {
			otx_html_add_breadcrumb($this->_('breadcrumb_add'));
			otx_html_response_title($this->_('breadcrumb_add'));
			otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'));
			otx_html_add_navtab($this->_('navtab_add_title'), $this->_('navtab_add_desc'), otx_router_url('add'), true);
		}
	}

	public function delete_action()
	{
		$group_id = otx_param_int('id');

		$sql = 'SELECT * FROM #system_group# WHERE group_id = ?';
		$row = otx_db_fetch_row($sql, $group_id);
		if (false == $row) {
			otx_admin_flash_warning($this->_('error_invalid_id', $group_id));
			otx_router_redirect('index');
		}

		otx_db_delete('#system_group#', array('group_id' => $group_id));

		otx_admin_flash_message($this->_('text_success_delete', $row));
		otx_admin_log_action($this->_('text_success_delete', $row));
		otx_router_redirect('index');
	}

	public function permissions_action()
	{
		$group_id = otx_param_int('id');

		$sql = 'SELECT * FROM #system_group# WHERE group_id = ?';
		$row = otx_db_fetch_row($sql, $group_id);
		if (false == $row) {
			otx_admin_flash_warning($this->_('error_invalid_id', $group_id));
			otx_router_redirect('index');
		}

		$permissions_event = otx_event_dispatch('admin_permissions', array('admin_permissions' => array()));
		$all_permissions = $permissions_event->admin_permissions;

		if (otx_request_is_post()) {
			$group_permissions = array();

			foreach ($all_permissions as $group_name => $group_info) {
				foreach ($group_info['items'] as $permission_name => $permission_info) {
					if (otx_param_exists($permission_name)) {
						$group_permissions[] = $permission_name . '=' . otx_param_int($permission_name);
					}
				}
			}

			otx_db_update('#system_group#', array('group_permissions' => implode("\r\n", $group_permissions)), array('group_id' => $group_id));

			otx_admin_flash_message($this->_('text_success_permissions', $row));
			otx_admin_log_action($this->_('text_success_permissions', $row));
			otx_router_redirect('index');
		} else {
			$group_permissions = otx_string_unserialize($row['group_permissions']);
		}

		$form_warpper = otx_html_create_wrapper(null, null, $this->_('button_permissions'));
		$form_table = $form_warpper->create_table($this->_('heading_permissions'), 'table-form');
		foreach ($all_permissions as $group_name => $group_info) {
			$permission_input = '';
			foreach ($group_info['items'] as $permission_name => $permission_info) {
				$attribs = array('label' => $permission_info['title'], 'label-class' => 'label-checkbox');
				if (isset($group_permissions[$permission_name]) && $group_permissions[$permission_name]) {
					$attribs['checked'] = 'checked';
				}

				$permission_input .= otx_html_form_checkbox($permission_name, 1, $attribs);
			}
			$form_table->add_form_entry($group_info['title'], $permission_input);
		}

		otx_html_add_breadcrumb($this->_('breadcrumb_index'), otx_router_url('index'));
		otx_html_add_breadcrumb($this->_('breadcrumb_permissions', $row));
		otx_html_response_title($this->_('breadcrumb_permissions', $row));
		otx_html_add_navtab($this->_('navtab_permissions_title'), $this->_('navtab_permissions_desc'), otx_request_uri(), true);
	}
}